/*
 * AbstractHPMDelta.java
 */
package OrderedCoverPackage;

import OLDSTUFF.ClassAlgebra.ComplexExpression;
import OLDSTUFF.ClassAlgebra.Expression;
import OLDSTUFF.ClassAlgebra.ListExpression;
import OLDSTUFF.ClassAlgebra.SimpleExpression;
import OLDSTUFF.DataSetPackage.DataSetException;
import OLDSTUFF.DataSetPackage.View;
import Estimator.OLDSTUFF_EstimatorExp;
import OLDSTUFF.HierarchyPackage.Hierarchy;
import OLDSTUFF.HierarchyPackage.HierarchyException;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * Abstract-class for XHPM-deltas implementing some common functionality.
 * The most important and complicated method is 'reCalculate' which is used to recalculate values for partitions to the right
 * of the altered partitions.
 * Note that 'addedDP' and 'removedDP' must be updated correctly by the specific implementations of the delta
 * in order for recalculate to word properly.
 * @author Martin Haulrich
 */
public abstract class AbstractHPMDelta extends AbstractTrainOC {

    /**
     * The cover that this cover is an delta upon.
     */
    final protected AbstractTrainOC parentCover;

    /** 
     * View on the data that are relevant for this cover.
     */
    final protected View view;

    /**
     * Map of partitions that have had datapoints added as a result of the delta-operation
     */
    final protected Map<OCPartition, Set<Integer>> addedDP;

    /**
     * Map of partitions that have had datapoints removed as a result of the delta-operation.
     */
    final protected Map<OCPartition, Set<Integer>> removedDP;

    /**
     * Map of partitions that have changed in this delta (compared to the parentCover).
     */
    final protected Map<OCPartition, OCPartition> changed;

    /**
     * BumpPostMDL for this cover
     */
    protected double BumpPostMDL;

    /**
     * Constructor common for all Delta-Covers.
     * @param parentCover The cover that this is a 'delta' of.
     * @param hier The hierarchy the cover is based on.
     * @param view View on datapoints.
     * @param prior Estimator for prior-probabilities.
     * @param dataPointProbs Probabilities for all datapoints in conver.
     * @throws DataSetPackage.DataSetException
     * @throws HierarchyPackage.HierarchyException
     */
    protected AbstractHPMDelta(AbstractTrainOC parentCover, Hierarchy hier, View view,
            OLDSTUFF_EstimatorExp prior, List<Double> dataPointProbs) throws DataSetException, HierarchyException {
        super(hier, prior);
        this.parentCover = parentCover;

        this.view = view;

        this.priorDataPointProbs = dataPointProbs;
        addedDP = new HashMap<OCPartition, Set<Integer>>(0);
        removedDP = new HashMap<OCPartition, Set<Integer>>(1);
        changed = new HashMap<OCPartition, OCPartition>();

        this.partitionList = new LinkedList<OCPartition>(parentCover.partitionList());
    }

    @Override
    protected int nextID() {
        return parentCover.nextID();
    }

    @Override
    public OCPartition getTop() {
        return parentCover.getTop();
    }

    @Override
    public int dataSize() {
        return parentCover.dataSize();
    }
}
